package ar.com.android.restoapp.providers;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import ar.com.android.restoapp.dbHelpers.SQLFoodOrderHelper;
import ar.com.android.restoapp.entities.TypeFood;

public class TypeFoodProvider {
	private static final String TAG = "TypeFoodProvider";
	
	private static final String DB_COLUMN_0_NAME = "idTypeFood";
	private static final String DB_COLUMN_1_NAME = "typeDescription";
	private static final String DB_COLUMN_2_NAME = "idMenuFood";

	private SQLiteDatabase database;
	private SQLFoodOrderHelper dbHelper;
	private String[] allColumns = {DB_COLUMN_0_NAME, DB_COLUMN_1_NAME, DB_COLUMN_2_NAME};
	
	public TypeFoodProvider(Context context) {
		dbHelper = SQLFoodOrderHelper.getHelper(context);
		database = dbHelper.getWritableDatabase();
	}
	
	public void dbOpen() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}
	
	public void dbClose() {
		dbHelper.close();
	}
	
	public long insertTypeFood(TypeFood typeFood)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_0_NAME, typeFood.getIdTypeFood());
		contentValues.put(DB_COLUMN_1_NAME, typeFood.getTypeDescription());
		contentValues.put(DB_COLUMN_2_NAME, typeFood.getIdMenuFood());
		
		long insertId = database.insert(SQLFoodOrderHelper.DB_TYPEFOOD_TABLE_NAME, null, contentValues);
		return insertId;
	}
	
	public boolean removeTypeFood(Integer idTypeFood)
	{
		int result = database.delete(SQLFoodOrderHelper.DB_TYPEFOOD_TABLE_NAME, DB_COLUMN_0_NAME + " = " + idTypeFood, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public boolean removeTypeFood()
	{
		int result = database.delete(SQLFoodOrderHelper.DB_TYPEFOOD_TABLE_NAME, null, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public long updateTypeFood(TypeFood typeFood)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_1_NAME, typeFood.getTypeDescription());
		contentValues.put(DB_COLUMN_2_NAME, typeFood.getIdMenuFood());
		
		return database.update(SQLFoodOrderHelper.DB_TYPEFOOD_TABLE_NAME, contentValues, DB_COLUMN_0_NAME + " = " + typeFood.getIdTypeFood(), null);
	}
	
	public List<TypeFood> getAllTypeFood() 
	{
		Cursor cursor = null;
		List<TypeFood> typeFoods = new ArrayList<TypeFood>();
		
		try
		{
			cursor = database.query(SQLFoodOrderHelper.DB_TYPEFOOD_TABLE_NAME, allColumns, null, null, null, null, null);
			cursor.moveToFirst();
			
			while (!cursor.isAfterLast()) {
				TypeFood typeFood = new TypeFood();
				typeFood.setIdTypeFood(cursor.getInt(0));
				typeFood.setTypeDescription(cursor.getString(1));
				typeFood.setIdMenuFood(cursor.getInt(2));
				
				typeFoods.add(typeFood);
				cursor.moveToNext();
			}
			return typeFoods;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return typeFoods;
		}
		finally {
			cursor.close();
		}
	}
	
	public List<TypeFood> getAllTypeFood(Integer idMenuFood) 
	{
		Cursor cursor = null;
		List<TypeFood> typeFoods = new ArrayList<TypeFood>();
		
		try
		{
			cursor = database.query(SQLFoodOrderHelper.DB_TYPEFOOD_TABLE_NAME, allColumns, DB_COLUMN_2_NAME + " = " + idMenuFood, null, null, null, null);
			cursor.moveToFirst();
			
			while (!cursor.isAfterLast()) {
				TypeFood typeFood = new TypeFood();
				typeFood.setIdTypeFood(cursor.getInt(0));
				typeFood.setTypeDescription(cursor.getString(1));
				typeFood.setIdMenuFood(cursor.getInt(2));
				
				typeFoods.add(typeFood);
				cursor.moveToNext();
			}
			return typeFoods;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return typeFoods;
		}
		finally {
			cursor.close();
		}
	}
}
